package by.vkhalko.asamo.services.impl;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.vkhalko.asamo.dataaccess.WorkDao;
import by.vkhalko.asamo.datamodel.Work;
import by.vkhalko.asamo.services.WorkService;

@Service
public class WorkServiceImpl implements WorkService {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(WorkServiceImpl.class);

	@Inject
	private WorkDao dao;

	@PostConstruct
	private void init() {
		// this method will be called by Spring after bean instantiation. Can be
		// used for any initialization process.
		LOGGER.info("Instance of WorkService is created. Class is: {}",
				getClass().getName());
	}

	@Override
	public Work get(Long id) {
		Work entity = dao.getById(id);
		return entity;
	}

	@Override
	public void saveOrUpdate(Work work) {
		if (work.getId() == null) {
			LOGGER.debug("Save new: {}", work);
			dao.insert(work);
		} else {
			LOGGER.debug("Update: {}", work);
			dao.update(work);
		}
	}

	@Override
	public void delete(Work work) {
		LOGGER.debug("Remove: {}", work);
		dao.delete(work.getId());

	}

	@Override
	public void deleteAll() {
		LOGGER.debug("Remove all works");
		dao.deleteAll();
	}

}
